package com.quyunshuo.module.home.activity.splash

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewpager2.widget.ViewPager2
import com.quyunshuo.edwingoal.base.BaseApplication
import com.quyunshuo.edwingoal.base.utils.BarUtils.addMarginTopEqualStatusBarHeight
import com.quyunshuo.edwingoal.common.ui.BaseActivity
import com.quyunshuo.module.home.activity.main.MainActivity
import com.quyunshuo.module.home.activity.main.SharedViewModel
import com.quyunshuo.module.home.activity.main.StepCounterManager
import com.quyunshuo.module.home.activity.splash.compose.SplashPage
import com.quyunshuo.module.home.adapter.ViewPagerAdapter
import com.quyunshuo.module.home.databinding.HomeActivityMainBinding
import com.quyunshuo.module.home.databinding.HomeActivitySplashBinding
import com.quyunshuo.module.home.util.AppExit
import com.quyunshuo.module.home.util.PermissionHelper
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class SplashActivity : BaseActivity<HomeActivitySplashBinding, SplashViewModel>() {
    companion object {
        fun newInstance(context: Context) {
            context.startActivity(Intent(context, SplashActivity::class.java))
        }
    }

    /**
     * 通过 viewModels() + Hilt 获取 ViewModel 实例
     */
    override val mViewModel by viewModels<SplashViewModel>()


    override fun createVB() = HomeActivitySplashBinding.inflate(layoutInflater)
    override fun HomeActivitySplashBinding.initListener() {

    }


    override fun HomeActivitySplashBinding.initView() {
        root.setContent {
            SplashPage{
                //先获得权限，获得以后，再进行跳转。
                getPermission()
            }
        }
    }

    fun getPermission(){
        requestActivityRecognitionPermission()
    }


    override fun initObserve() {

    }


    override fun initRequestData() {

    }

    override fun onBackPressed() {
        AppExit.onBackPressed(this)
    }


    private fun requestActivityRecognitionPermission() {
        PermissionHelper.requestActivityRecognitionPermission(this@SplashActivity,onGranted = {
            // 活动识别权限授予后，检查通知权限
            checkNotificationPermission()

        }, onDenied = {
            // 活动识别权限拒绝后，仍然检查通知权限
            checkNotificationPermission()
        })
    }
    private fun checkNotificationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            // Android 13+ 使用官方通知权限API
            if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
                requestNotificationPermission()
            } else {
                goNext()
            }
        } else {
            // 对于 Android 13 以下版本，通知权限默认视为已授予
            goNext()
        }
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private fun requestNotificationPermission() {
        //请求权限
        requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
    }

    //处理请求权限结果
    private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        // 无论通知权限是否授予，都执行回调
        goNext()
    }

    fun goNext(){
        MainActivity.newInstance(this@SplashActivity)
    }
}